﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using BusinessLayer;
using Library;

public partial class Administrator_ProducerEdit : System.Web.UI.Page
{
    string cmd = "";
    int Id = 0;
    protected void Page_Load(object sender, EventArgs e)
    {
        cmd = Request.QueryString["cmd"].ToString();
        switch (cmd)
        {
            case "new":
                rbStatus.Enabled = false;
                btnNew.Enabled = true;
                btnUpdate.Enabled = false;
                break;
            case "edit":
                rbStatus.Enabled = true;
                btnNew.Enabled = false;
                btnUpdate.Enabled = true;
                Id = Convert.ToInt32(Request.QueryString["Id"].ToString());
                if (!IsPostBack)
                {
                    FillData(Producer.GetOne(Id));
                }
                break;
        }
    }
    protected void txtTen_TextChanged(object sender, EventArgs e)
    {

    }

    protected void btnNew_Click(object sender, EventArgs e)
    {
        Producer producer = new Producer();
        producer.Producer_Name = txtTen.Text;
        producer.Producer_Website = txtMota.Text;
        try
        {
            Producer.Insert(producer);
            MessageBox.Show("Hãng đã được thêm thành công!");
            Response.Redirect("Producer.aspx");
        }
        catch (Exception ex)
        {
            MessageBox.Show("Lỗi !!!! Mời bạn thử lại!");
            throw ex;
        }
        
    }

    protected void btnUpdate_Click(object sender, EventArgs e)
    {
        Producer producer = new Producer();
        producer.Producer_ID = Id;
        producer.Producer_Name = txtTen.Text;
        producer.Producer_Website = txtMota.Text;
        producer.Status = 0;

        try
        {
            Producer.Update(producer);
            MessageBox.Show("Hãng đã được sửa thành công!");
            Response.Redirect("ProducerEdit.aspx?cmd=edit&id=" + Id.ToString());
        }
        catch (Exception ex)
        {
            MessageBox.Show("Lỗi !!!! Mời bạn thử lại!");
            throw ex;
        }
    }

    protected void BbtnBack_Click(object sender, EventArgs e)
    {
        Response.Redirect("Producer.aspx");
    }

    protected void FillData(Producer producer)
    {
        txtTen.Text = producer.Producer_Name;
        txtMota.Text = producer.Producer_Website;
        rbStatus.Items.FindByValue(producer.Status.ToString()).Selected = true;
    }
}